package com.tcoiss.datafactory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.core.enums.ErroEnums;
import com.tcoiss.common.core.exception.CustomException;
import com.tcoiss.common.core.utils.DateUtils;
import com.tcoiss.common.component.api.RemoteApiService;
import com.tcoiss.common.component.api.model.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.tcoiss.datafactory.mapper.BillTypeMapper;
import com.tcoiss.datafactory.domain.BillType;
import com.tcoiss.datafactory.service.IBillTypeService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 单据类型配置Service业务层处理
 *
 * @author zw
 * @date 2021-05-27
 */
@Service
public class BillTypeServiceImpl extends ServiceImpl<BillTypeMapper, BillType> implements IBillTypeService {
    @Autowired
    private RemoteApiService remoteApiService;
    @Autowired
    private IBillTypeService iBillTypeService;
    @Override
    public List<BillType> queryList(BillType billType) {
        LambdaQueryWrapper<BillType> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(billType.getBillTypeCode())){
            lqw.eq(BillType::getBillTypeCode ,billType.getBillTypeCode());
        }
        if (StringUtils.isNotBlank(billType.getBillTypeName())){
            lqw.like(BillType::getBillTypeName ,billType.getBillTypeName());
        }
        if (billType.getStatus() != null){
            lqw.eq(BillType::getStatus ,billType.getStatus());
        }
        return this.list(lqw);
    }

    @Override
    public boolean syncBillType() {
        ApiParam apiParam = new ApiParam();
        apiParam.setApiCode("syncBillType");
        String select = "id,number,name,isdefault,enable,createtime,modifytime";
        Map<String,Object> param = new HashMap<>();
        param.put("select",select);
        apiParam.setParam(param);
        R<Map<String, Object>> r = remoteApiService.executeKdApi(apiParam);
        List<List<Object>> rows;
        Map<String, Object> map = r.getData();
        List<BillType> mtList = new ArrayList<>();
        if (r.getCode() == 200) {
            if (map.get("success").equals("true")) {
                Map<String, Object> data = (Map<String, Object>) map.get("data");
                rows = (List) data.get("rows");
                int count = Integer.valueOf(data.get("count").toString());
                if (count == 0) {
                    throw new CustomException(ErroEnums.DATAERRO.getCode(), null, "未查询到可同步数据");
                }
                if (count > 0) {
                    for (List<Object> row : rows) {
                        BillType bt = new BillType();
                        bt.setBillTypeCode(row.get(1).toString());
                        bt.setBillTypeName(row.get(2).toString());
                        bt.setIsDefault(row.get(3).toString());
                        bt.setStatus(row.get(4).toString());
                        bt.setCreateTime(DateUtils.strParseToDate(row.get(5)));
                        bt.setModifyTime(DateUtils.strParseToDate(row.get(6)));
                        mtList.add(bt);
                    }
                    iBillTypeService.remove(new QueryWrapper<>());
                    return iBillTypeService.saveBatch(mtList);
                }
            } else {
                throw new CustomException( ErroEnums.APIERRO.getCode(), new Object[]{apiParam}, ErroEnums.APIERRO.getInfo()+": " + r.getMsg());
            }
        }
        throw new CustomException(ErroEnums.COMERRO.getCode(), null,r.getMsg());
    }
}
